package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.service.UserService;
import com.cskaoyan.service.UserServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.ResponseUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author stone
 * @date 2023/04/05 15:17
 */
@WebServlet("/admin/user/*")
public class AdminUserServlet extends WdBaseServlet {

    UserService userService = new UserServiceImpl();

    public void list(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        String order = request.getParameter("order");
        String sort = request.getParameter("sort");
        String username = request.getParameter("username");
        String mobile=request.getParameter("mobile");


        CommonData<MarketUser> data = userService.query(page,limit,sort,order,username,mobile);
        ResponseUtil.responseJson(response, BaseRespVo.ok(data));
    }
    public BaseRespVo detail(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer id = request.getParameter("id") == null ? null : Integer.parseInt(request.getParameter("id"));

        BaseRespVo data = userService.queryDetail(id);
        return data;
    }
    public void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jsonStr = request.getReader().readLine();
        MarketUser marketUser = JacksonUtil.read(jsonStr, MarketUser.class);
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketUserMapper mapper = sqlSession.getMapper(MarketUserMapper.class);
        int i = mapper.updateByPrimaryKey(marketUser);
        sqlSession.commit();
        sqlSession.close();
        ResponseUtil.responseOkJsonVo(response,i);
    }

}
